#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

// int find(vector<long> &dist, vector<int> &father, int x)
// {
// 	if (father[x] != x) {
// 		int p = father[x];
// 		father[x] = find(dist, father, p);
// 		dist[x] += dist[p];
// 	}
// 	return father[x];
// }

// int unite(vector<long> &dist, vector<int> &father, int x, int y, int val)
// {
// 	int fx = find(dist, father, x);
// 	int fy = find(dist, father, y);
// 	if (fx != fy) {
// 		father[fx] = fy;
// 		dist[fx] = val + dist[y] - dist[x];
// 		return 1;
// 	}
// 	return 0;
// }

// bool check(vector<long> &dist, vector<int> &father, int l, int r, int v)
// {
// 	if (find(dist, father, l) == find(dist, father, r)) {
// 		if ((dist[l] - dist[r]) != v) {
// 			return false;
// 		}
// 	}
// 	return true;
// }
int find(int x, vector<int> &father, vector<long> &rank)
{
	if (father[x] != x) {
		int tmp = father[x];
		father[x] = find(father[x], father, rank);
		rank[x] += rank[tmp];
	}
	return father[x];
}

int merge(int l, int r, long val, vector<int> &father, vector<long> &rank)
{
	int fl = find(l, father, rank);
	int fr = find(r, father, rank);
	if (fl != fr) {
		father[fl] = fr;
		rank[fl] = val + rank[r] - rank[l];
		return 1;
	}
	return 0;
}

int check(int l, int r, long val, vector<int> &father, vector<long> &rank)
{
	int fl = find(l, father, rank);
	int fr = find(r, father, rank);
	if (fl == fr) {
		if ((rank[l] - rank[r]) != val) {
			return false;
		}
	}
	return true;
}

int main()
{
	int n = 0, m = 0, ans = 0;
	while (cin >> n >> m) {
		vector<long> dist(n + 2, 0);
		vector<int> father(n + 2, 0);
		iota(father.begin(), father.end(), 0);
		ans = 0;
		
		for (int i = 0; i < m; i++) {
			long x, y, val;
			cin >> x >> y >> val;
			y++;
			if (!check(x, y, val, father, dist)) {
				ans++;
			} else {
				merge(x, y, val, father, dist);
			}
		}
		cout << ans << endl;
	}

	return 0;
}